<template>
  <div class="admin-user-wrapper">
    <el-card class="box-card">
      <el-table border :data="userlist" style="width: 100%" :default-sort="{ prop: 'state', order: 'descending' }">
        <el-table-column prop="id" label="ID" width="300" fixed />
        <el-table-column prop="username" label="昵称" width="180" />
        <el-table-column prop="photo" label="头像">
          <template v-slot="scope">
            <img :src="scope.row.photo" alt="" style="width: 50px; height: 50px; border-radius: 50%;" />
          </template>
        </el-table-column>
        <el-table-column prop="account" label="账号" />
        <el-table-column prop="sex" label="性别" />
        <el-table-column prop="state" label="状态" sortable>
          <template v-slot="scope">
            <el-tag v-if="scope.row.state == 2" type="success">正常</el-tag>
            <el-tag v-else-if="scope.row.state == 1" type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="editor(scope.row.id,scope.row.state)">{{isDisabled(scope.row.state)}}</el-button>
            <el-button type="danger" size="mini" @click="deleteUser(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

  </div>
</template>

<script>
import { ElMessage, ElMessageBox } from 'element-plus'
export default {
  name: 'AdminUser',
  data() {
    return {
      userlist: []
    }
  },
  mounted() {
    this.getUserList();
  },
  methods: {

    getUserList() {
      this.$axios.post('/users/getuserlist', {}).then(res => {
        const data = res.data;
        if (data.code === 200) {
          this.userlist = data.data;
        } else {
          console.log('Q3wertgh')
          alert('获取用户列表失败')
        }
      })
    },
    isDisabled(state) {
      switch (parseInt(state)) {
        case 2:
          return '禁用';
        case 1:
          return '启用';
      }
    },
    deleteUser(id) {
      ElMessageBox.confirm(
        '是否删除该用户?',
        'Warning',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消删除',
          type: 'warning',
        }).then(() => {
          this.$axios.post('/users/deleteuser', { id }).then(res => {
            if (res.data.code === 200) {
              ElMessage({
                type: 'success',
                message: res.data.msg,
              })
              this.getUserList();
            } else {
              ElMessage({
                type: 'error',
                message: res.data.msg,
              })
            }

          })
        })
    },
    editor(id, state) {
      if (state == 2) {
        state = 1;
      } else {
        state = 2;
      }
      this.$axios.post('/users/editorstate', {
        id,
        state
      }).then(res => {
        const data = res.data;
        if (data.code === 200) {
          this.getUserList();
        } else {
          alert('修改失败')
        }
      })
    }
  }
}
</script>

<style scoped>
.admin-user-wrapper {
  width: 100%;
  height: 100%;
  background-color: #ffffff;
  padding: 10px;
}

.box-card {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
</style>